using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class MasterWorkbench
    {
        Dictionary<string, RapidHardware> dicRapidHardwares;
        Settings oSettings;
        bool bUseMasterSettings = true;


        public bool UseMasterSettings
        {
            get { return bUseMasterSettings; }
            set { bUseMasterSettings = value; }
        }

        public Settings Settings
        {
            get { return oSettings; }
        }

        public virtual RapidHardware BuildRapidHardware()
        {
            return null;
        }

        public virtual bool BuildRapidHardwares()
        {
            return true;
        }

        public void RegisterRapidHardware(string psName, RapidHardware poRapidHardware)
        {
            if (dicRapidHardwares.ContainsKey(psName))
            {
                dicRapidHardwares[psName] = poRapidHardware;
            }
            else
            {
                dicRapidHardwares.Add(psName, poRapidHardware);
            }
        }

        public virtual void ConfigureSettings()
        {
        }

        public MasterWorkbench()
        {
            oSettings = new Settings();
            ConfigureSettings();

            dicRapidHardwares = new Dictionary<string, RapidHardware>();
            BuildRapidHardwares();
            RapidHardware oDefaultHardware = BuildRapidHardware();
            if (oDefaultHardware != null)
                RegisterRapidHardware("default", oDefaultHardware);
        }

        public RapidHardware this[string sName]
        {
            get
            {
                return dicRapidHardwares[sName];
            }
        }

        public Dictionary<string,RapidHardware> RapidHardwares
        {
            get
            {
                return dicRapidHardwares;
            }
        }

        public void ShowWorkbench()
        {
            string sHardware = "default";
            if (dicRapidHardwares.Count == 1)
            {
                foreach (string sKey in dicRapidHardwares.Keys)
                {
                    sHardware = sKey;
                }
            }

            if (!dicRapidHardwares.ContainsKey(sHardware))
            {
                FSelectHardware frmSelectHardware = new FSelectHardware();
                frmSelectHardware.Initialize(this);
                System.Windows.Forms.Application.Run(frmSelectHardware);
                sHardware = frmSelectHardware.SelectedHardware;
                if (sHardware == "")
                    return;
            }
            ShowHardware(sHardware);
        }

        public void ShowHardware(string psRapidHardwareName)
        {
            RapidHardware oRapidHardware = this[psRapidHardwareName];
            if (oRapidHardware == null)
                throw new Exception("hardware named " + psRapidHardwareName + " does not exist");

            if (bUseMasterSettings)
                oRapidHardware.ReplaceSettings(oSettings);
            oRapidHardware.StartRapidHardware();
            oRapidHardware.LaunchGUI(true);
        }


    }
}
